package com.paomi.client.activity.message;

import android.app.Activity;

import com.paomi.client.R;
import com.paomi.client.activity.message.model.MyPraiseBean;
import com.paomi.client.base.BaseLoadMorePresenter;
import com.paomi.client.base.BaseMvpLCEView;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.net.Api;
import com.paomi.client.net.BaseCallback;
import com.paomi.client.net.Constants;
import com.paomi.client.utils.ToastUtil;
import com.paomi.client.widget.RxToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * Created by pc on 2018/11/9
 * Describe:
 * com.paomi.client.activity.reply
 */
public class PraiseListPresenter extends BaseLoadMorePresenter<BaseMvpLCEView> {
    private List<MyPraiseBean.PraiseBean> commentList=new ArrayList<>();
    private int pageNum = 0;
    private Activity activity;

    /**
     * 请求回复列表接口
     * * pageNum	true	页码
     * * pageSize	true	页记录数
     * * sort	true	0 时间 1 点赞数
     */
    public void setPraiseContent(int page,Activity activity) {
        this.activity = activity;
        this.pageNum = page;
        loadData(true);
    }
    @Override
    protected void loadData(int page, int skip, boolean pullToRefresh) {
        if (pageNum != 0) {
            Map<String, String> maps = new HashMap<>();
//            maps.put("isRead", "0");
            maps.put("pageNum", "" + pageNum);
            maps.put("pageSize", "" + Constants.PAGE_SIZE);
            Api.getInstanceService(Constants.Ticket_URL).myPraiseList(maps).enqueue(new BaseCallback<MyPraiseBean>() {
                @Override
                public void onResponse(Call<MyPraiseBean> call, MyPraiseBean result) {
                    if (pageNum == 1) {
                        commentList.clear();
                    }
//                    commentList.addAll(result.getReturnData().getData());
                    if(commentList.size()>0){
                        //判断是否有重复数据
                        boolean isData=false;
                        List<MyPraiseBean.PraiseBean>  comments1=result.getReturnData().getData();
                        for(MyPraiseBean.PraiseBean comment:comments1){
                            for(int i=0;i<commentList.size();i++){
                                if(comment.getId() == commentList.get(i).getId()){
                                    isData=false;
                                    break;
                                }else{
                                    isData=true;
                                }
                            }
                        }
                        if(isData){
                            commentList.addAll(result.getReturnData().getData());
                        }else{
                            ToastUtil.getToastEmail().ToastShow(activity, null, 1, R.layout.toast_view, activity.getResources().getString(R.string.data_null));
                        }
                    }else{
                        commentList.addAll(result.getReturnData().getData());
                    }
                    if(isViewAttached())
                    getView().setData(commentList, false);
                }
            });
        }else {
            if(isViewAttached())
            getView().setData(commentList,false);
        }
    }

    /**
     * 点击已读
     * @param id
     */
    public void clickReaded(String id){
        Api.getInstanceService(Constants.Ticket_URL).readPraiseList(id).enqueue(new BaseCallback<BaseJSON>() {
            @Override
            public void onResponse(Call<BaseJSON> call, BaseJSON result) {
//                RxToast.info("点击已读");
                loadData(true);
            }
        });
    }
}
